﻿using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using MessageBox = System.Windows.MessageBox;
using TUlingWpf.Heplers;
using TUlingWpf.Models;
using TUlingWpf.Services;
using CommunityToolkit.Mvvm.Messaging;
using TUlingWpf.Messages;

namespace TUlingWpf.ViewModels
{
    public partial class LoginViewModel(UserSession session) : ObservableObject
    {
        public UserSession Session { get; set; } = session;

        [ObservableProperty]
        private string username;

        [ObservableProperty]
        private string password;

        [RelayCommand]
        private void Login()
        {
            if (string.IsNullOrEmpty(Username)||string.IsNullOrEmpty(Password))
            {
                return;
            }
            var res = SqlSugarHelper.Db.Queryable<User>().AS("用户表").ToList().Where(s => s.Name==Username&&s.Password==Password).ToList();
            if (res.Count>0)
            {
                //登录成功
                Session.CurrentUser = res[0];
                //进行跳转 toolkit的信使功能
                WeakReferenceMessenger.Default.Send(new LoginMessage(res[0]));
            }
            else
            {
                MessageBox.Show("用户名或密码错误");
            }
        }
    }
}